#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class AwardRcdProvider : CY.GFive.Core.Providers.DALProvider.IAwardRcdProvider
	{
		#region Sql
		private static readonly string SqlSelectAwardRcd = "SELECT [Id], [StaffCode],[AwardDate],[AwardCate],[AwardLvl],[Des] FROM [AwardRcd] WHERE  [Id]=@Id";

		private static readonly string SqlInsertAwardRcd = "INSERT INTO [AwardRcd]([StaffCode],[AwardDate],[AwardCate],[AwardLvl],[Des]) values(@StaffCode,@AwardDate,@AwardCate,@AwardLvl,@Des);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateAwardRcd = "UPDATE [AwardRcd] SET [StaffCode] = @StaffCode,[AwardDate] = @AwardDate,[AwardCate] = @AwardCate,[AwardLvl] = @AwardLvl,[Des] = @Des WHERE  [Id]=@Id";

		private static readonly string SqlDeleteAwardRcd = "DELETE [AwardRcd] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllAwardRcd = "SELECT [Id], [StaffCode],[AwardDate],[AwardCate],[AwardLvl],[Des] FROM [AwardRcd]";
		#endregion

		#region IAwardRcdProvider Members
		public Core.Business.AwardRcd Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectAwardRcd);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.AwardRcd awardRcd = new Core.Business.AwardRcd();

				if (!reader.IsDBNull(0)) awardRcd.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) awardRcd.StaffCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) awardRcd.AwardDate = reader.GetDateTime(2);
				if (!reader.IsDBNull(3)) awardRcd.AwardCate = reader.GetString(3);
				if (!reader.IsDBNull(4)) awardRcd.AwardLvl = reader.GetString(4);
				if (!reader.IsDBNull(5)) awardRcd.Des = reader.GetString(5);

				return awardRcd;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.AwardRcd awardRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StaffCode", SqlDbType.NVarChar, awardRcd.StaffCode);
			sql.AddParameter("@AwardDate", SqlDbType.DateTime, awardRcd.AwardDate);
			sql.AddParameter("@AwardCate", SqlDbType.NVarChar, awardRcd.AwardCate);
			sql.AddParameter("@AwardLvl", SqlDbType.NVarChar, awardRcd.AwardLvl);
			sql.AddParameter("@Des", SqlDbType.NVarChar, awardRcd.Des);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertAwardRcd);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) awardRcd.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.AwardRcd awardRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StaffCode", SqlDbType.NVarChar, awardRcd.StaffCode);
			sql.AddParameter("@AwardDate", SqlDbType.DateTime, awardRcd.AwardDate);
			sql.AddParameter("@AwardCate", SqlDbType.NVarChar, awardRcd.AwardCate);
			sql.AddParameter("@AwardLvl", SqlDbType.NVarChar, awardRcd.AwardLvl);
			sql.AddParameter("@Des", SqlDbType.NVarChar, awardRcd.Des);
			sql.AddParameter("@Id", SqlDbType.Int, awardRcd.Id);
			sql.ExecuteSql(SqlUpdateAwardRcd);
		}

		public void Delete(Core.Business.AwardRcd awardRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, awardRcd.Id);
			sql.ExecuteSql(SqlDeleteAwardRcd);
		}

		public IList<Core.Business.AwardRcd> GetAllAwardRcd()
		{
			IList<Core.Business.AwardRcd> awardRcdlist = new List<Core.Business.AwardRcd>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllAwardRcd);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.AwardRcd awardRcd = new Core.Business.AwardRcd();

					if (!reader.IsDBNull(0)) awardRcd.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) awardRcd.StaffCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) awardRcd.AwardDate = reader.GetDateTime(2);
					if (!reader.IsDBNull(3)) awardRcd.AwardCate = reader.GetString(3);
					if (!reader.IsDBNull(4)) awardRcd.AwardLvl = reader.GetString(4);
					if (!reader.IsDBNull(5)) awardRcd.Des = reader.GetString(5);

					awardRcd.MarkOld();
					awardRcdlist.Add(awardRcd);
				}
				reader.Close();
			}
			return awardRcdlist;
		}
		#endregion
	}
}
